﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// Summary description for imsitem
/// </summary>
public class imsitem
{
   public  string id = System.Guid.NewGuid().ToString();
   public  string parentID = null;

   public  string title = "";
   public  string href = "";

   public imsitem parent = null;
   public List<imsitem> childs = null;

   public imsresource objResource = new imsresource ();


    /// <summary>
    /// this function will get a list of Ims xml file items
    /// </summary>
    /// <param name="strPath">the path of the file</param>
    /// <returns>a list of type imsitem which contains all needed data</returns>
    public static List<imsitem> getListOfFile(string strPath)
    {

        //Dictionary<string, string> dlNew = new Dictionary<string, string>();


        List<imsresource> lstResources = new List<imsresource>();



        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strPath);

        XmlNodeList xmlist = xmlDoc.GetElementsByTagName("resources");

        if (xmlist.Count == 0)
            return new List<imsitem>();


        XmlNode xmlResources = xmlist[0];



        foreach (XmlNode xn in xmlResources.ChildNodes)
        {
            if (xn.Name == "resource")
            {
                //if (xn.Attributes["href"] != null)
                //    dlNew.Add(xn.Attributes["identifier"].Value, xn.Attributes["href"].Value);
                //else
                //    dlNew.Add(xn.Attributes["identifier"].Value, "");


                imsresource imsResource =   imsresource. getResourceFromNode(xn);

                lstResources.Add(imsResource);

            }
        }


        xmlist = xmlDoc.GetElementsByTagName("organizations");


        if (xmlist.Count == 0)
            return new List<imsitem>();



        List<imsitem> lstRet = new List<imsitem>();

        foreach (XmlNode xn in xmlist[0].ChildNodes)
        {
            if (xn.Name == "organization")
            {
                imsitem imsNew = new imsitem();
                imsNew.href = "";
                foreach (XmlNode xnTitle in xn.ChildNodes)
                {
                    if (xnTitle.Name == "title")
                    {
                        imsNew.title = xnTitle.ChildNodes[0].Value;
                        break;
                    }
                }

                imsNew.parent = null;

                lstRet.Add(imsNew);

                buildOrganization(lstRet, imsNew, xn,lstResources);
                
            }


        }


        List<imsitem> lstNewRet = new List<imsitem>();

        foreach (imsitem it in lstRet)
        {
            if (it.parent == null)
            {
                lstNewRet.Add(it);
            }
        }




        return lstNewRet;
    }



    



    /// <summary>
    /// this function will build all organization contents from items and add it to list
    /// </summary>
    /// <param name="lstRet">the list to add items to</param>
    /// <param name="imsNew">the parent item</param>
    /// <param name="xn">the node that started it</param>
    /// <param name="dlNew">this is a list of all resources in the file</param>
    private static void buildOrganization(List<imsitem> lstRet, imsitem imsNew, XmlNode xn,List<imsresource> dlNew)
    {
        foreach (XmlNode xn1 in xn.ChildNodes)
        {
            if (xn1.Name == "item")
            {
                imsitem imsNew1 = new imsitem();
                imsNew1.href = "";

                if (xn1.Attributes["identifierref"] != null)
                {

                    List<imsresource> lstFound = new List<imsresource>(from obj in dlNew where obj.identifier == xn1.Attributes["identifierref"].Value select obj);


                    if (lstFound.Count > 0)
                    {
                        imsNew1.href = lstFound[0].href;
                        imsNew1.objResource = lstFound[0];


                    }
                }

                foreach (XmlNode xnTitle in xn1.ChildNodes)
                {
                    if (xnTitle.Name == "title")
                    {
                        imsNew1.title = xnTitle.ChildNodes[0].Value;
                        break;
                    }
                }

                imsNew1.parent = imsNew;
                imsNew1.parentID = imsNew.id;


               

                if (imsNew.childs == null)
                    imsNew.childs = new List<imsitem>();
                imsNew.childs.Add(imsNew1);
                lstRet.Add(imsNew1);


                buildOrganization(lstRet, imsNew1, xn1,dlNew);

            }


        }
    }


	public imsitem()
	{
		//
		// TODO: Add constructor logic here
		//
	}
}